<?php

namespace App\Models\Depot;

use Illuminate\Database\Eloquent\Model;
use App\Models\Region as RegionModel;

class Back extends Model
{
    protected $table = 'depot_back';

    protected $guarded = [];

    protected $dateFormat = 'U';

    const CREATED_AT = 'createtime';

    const UPDATED_AT = null;

    protected $appends = [
        'region_text', //地区
    ];

    public function getRegionTextAttribute()
    {
        $region = '';

        $province = RegionModel::where('code', $this->attributes['province'])->value('name');

        if ($province) {
            $region = $province;
        }

        $city = RegionModel::where('code', $this->attributes['city'])->value('name');

        if ($city) {
            $region .= $city;
        }

        $district = RegionModel::where('code', $this->attributes['district'])->value('name');

        if ($district) {
            $region .= $district;
        }

        return $region . $this->attributes['address'];
    }
}